

package com.yh.modules.sys.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yh.common.annotation.DataFilter;
import com.yh.common.constant.Constant;
import com.yh.modules.data.entity.MachineEntity;
import com.yh.modules.data.service.MachineService;
import com.yh.modules.sys.dao.SysDeptDao;
import com.yh.modules.sys.entity.SysDeptEntity;
import com.yh.modules.sys.service.SysDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("sysDeptService")
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class SysDeptServiceImpl extends ServiceImpl<SysDeptDao, SysDeptEntity> implements SysDeptService {

    @Autowired
    private MachineService machineService;

    @Override
    @DataFilter(subDept = true, user = false)
    public List<SysDeptEntity> queryList(Map<String, Object> params) {

        List<SysDeptEntity> deptList = this.selectList(new EntityWrapper<SysDeptEntity>()
                .addFilterIfNeed(params.get(Constant.SQL_FILTER) != null, (String) Constant.SQL_FILTER));


        for (SysDeptEntity sysDeptEntity : deptList) {
            //设置父部门名称
            setParentDeptName(sysDeptEntity);

            //设置机器数量
            //setMachineCount(sysDeptEntity);
        }

        Class<SysDeptServiceImpl> sysDeptServiceClass = SysDeptServiceImpl.class;
        try {
            Method queryList = sysDeptServiceClass.getMethod("queryList", Map.class);
            DataFilter annotation = queryList.getAnnotation(DataFilter.class);
//            log.info("注解:{}",annotation);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }


        return deptList;
    }

    @Override
    @DataFilter(subDept = true, user = false)
    public List<SysDeptEntity> queryListTest(Map<String, Object> params) {
        List<SysDeptEntity> sysDeptEntityList = this.selectList(new EntityWrapper<SysDeptEntity>()
                .addFilterIfNeed(params.get(Constant.SQL_FILTER) != null, (String) params.get(Constant.SQL_FILTER)));

        return sysDeptEntityList;
    }


    private void setMachineCount(SysDeptEntity sysDeptEntity) {
        List<MachineEntity> machineEntityList = machineService.getMachineListByDeptId(sysDeptEntity.getDeptId());
        if (CollUtil.isNotEmpty(machineEntityList)) {
            sysDeptEntity.setMachineCount(machineEntityList.size());
        } else {
            sysDeptEntity.setMachineCount(0);
        }
    }

    private void setParentDeptName(SysDeptEntity sysDeptEntity) {
        SysDeptEntity parentDeptEntity = this.selectById(sysDeptEntity.getParentId());
        if (parentDeptEntity != null) {
            sysDeptEntity.setParentName(parentDeptEntity.getName());
        } else {
            sysDeptEntity.setParentName("");
        }
    }

    @Override
    public List<Long> queryDetpIdList(Long parentId) {
        return baseMapper.queryDetpIdList(parentId);
    }

    @Override
    public List<Long> getSubDeptIdList(Long deptId) {
        //部门及子部门ID列表
        List<Long> deptIdList = new ArrayList<>();

        //获取子部门ID
        List<Long> subIdList = queryDetpIdList(deptId);
        getDeptTreeList(subIdList, deptIdList);

        return deptIdList;
    }

    @Override
    public String getDeptNameById(Long deptId) {
        SysDeptEntity sysDeptEntity = this.selectById(deptId);
        if (sysDeptEntity == null) {
            return "";
        }
        if (StrUtil.isEmpty(sysDeptEntity.getName())) {
            return "";
        } else {
            return sysDeptEntity.getName();
        }
    }

    /**
     * 递归
     */
    private void getDeptTreeList(List<Long> subIdList, List<Long> deptIdList) {
        for (Long deptId : subIdList) {
            List<Long> list = queryDetpIdList(deptId);
            if (list.size() > 0) {
                getDeptTreeList(list, deptIdList);
            }

            deptIdList.add(deptId);
        }
    }
}
